package org.example;

import java.sql.*;
import java.util.List;
import java.util.Scanner;

public class LibrarySystem {
    public static void main(String[] args) {
        try {
            DBUtil.createDatabase();
            createTables();
            Scanner scanner = new Scanner(System.in);
            StaffDAO staffDAO = new StaffDAO();
            BookDAO bookDAO = new BookDAO();

            while (true) {
                System.out.println("\n==zqw=== 图书馆管理系统 ==zqw===");
                System.out.println("1. 查询数据");
                System.out.println("2. 插入数据");
                System.out.println("3. 删除数据");
                System.out.println("4. 退出系统");
                System.out.print("请选择操作：");

                int choice;
                try {
                    choice = Integer.parseInt(scanner.nextLine().trim());
                } catch (NumberFormatException e) {
                    System.out.println("输入错误，请输入数字");
                    continue;
                }

                switch (choice) {
                    case 1:
                        handleQuery(scanner, staffDAO, bookDAO);
                        break;
                    case 2:
                        handleInsert(scanner, staffDAO, bookDAO);
                        break;
                    case 3:
                        handleDelete(scanner, staffDAO, bookDAO);
                        break;
                    case 4:
                        System.out.println("程序已退出。");
                        scanner.close();
                        return;
                    default:
                        System.out.println("无效选择，请输入1-4之间的数字");
                }
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void createTables() throws SQLException {
        try (Connection conn = DBUtil.getConnection();
             Statement stmt = conn.createStatement()) {

            String createStaffTable = "CREATE TABLE IF NOT EXISTS staff (" +
                    "card_id VARCHAR(20) PRIMARY KEY," +
                    "name VARCHAR(50) NOT NULL," +
                    "gender ENUM('男', '女') NOT NULL," +
                    "age INT NOT NULL," +
                    "id_number VARCHAR(18) NOT NULL UNIQUE," +
                    "create_time DATETIME NOT NULL" +
                    ")";
            stmt.executeUpdate(createStaffTable);

            String createBookTable = "CREATE TABLE IF NOT EXISTS book (" +
                    "book_id VARCHAR(20) PRIMARY KEY," +
                    "title VARCHAR(100) NOT NULL," +
                    "author VARCHAR(50) NOT NULL," +
                    "publish_year INT NOT NULL" +
                    ")";
            stmt.executeUpdate(createBookTable);

            System.out.println("表创建成功");
        }
    }

    private static void handleQuery(Scanner scanner, StaffDAO staffDAO, BookDAO bookDAO) throws Exception {
        System.out.println("请选择查询的表：");
        System.out.println("1. 人员表");
        System.out.println("2. 图书表");

        int tableChoice;
        try {
            tableChoice = Integer.parseInt(scanner.nextLine().trim());
        } catch (NumberFormatException e) {
            System.out.println("输入错误，请输入数字");
            return;
        }

        if (tableChoice == 1) {
            System.out.println("==zqw=== 人员表数据 ==zqw===");
            List<Staff> staffList = staffDAO.getAll();
            if (staffList.isEmpty()) {
                System.out.println("人员表暂无数据");
            } else {
                staffList.forEach(System.out::println);
            }
        } else if (tableChoice == 2) {
            System.out.println("==zqw=== 图书表数据 ==zqw===");
            List<Book> bookList = bookDAO.getAll();
            if (bookList.isEmpty()) {
                System.out.println("图书表暂无数据");
            } else {
                bookList.forEach(System.out::println);
            }
        } else {
            System.out.println("无效选择，请输入1或2");
        }
    }

    private static void handleInsert(Scanner scanner, StaffDAO staffDAO, BookDAO bookDAO) throws Exception {
        System.out.println("请选择插入的表：");
        System.out.println("1. 人员表");
        System.out.println("2. 图书表");

        int tableChoice;
        try {
            tableChoice = Integer.parseInt(scanner.nextLine().trim());
        } catch (NumberFormatException e) {
            System.out.println("输入错误，请输入数字");
            return;
        }

        if (tableChoice == 1) {
            staffDAO.insert(scanner);
        } else if (tableChoice == 2) {
            bookDAO.insert(scanner);
        } else {
            System.out.println("无效选择，请输入1或2");
        }
    }

    private static void handleDelete(Scanner scanner, StaffDAO staffDAO, BookDAO bookDAO) throws Exception {
        System.out.println("请选择删除的表：");
        System.out.println("1. 人员表");
        System.out.println("2. 图书表");

        int tableChoice;
        try {
            tableChoice = Integer.parseInt(scanner.nextLine().trim());
        } catch (NumberFormatException e) {
            System.out.println("输入错误，请输入数字");
            return;
        }

        if (tableChoice == 1) {
            System.out.print("请输入要删除的人员卡号：");
            String cardId = scanner.nextLine().trim();
            staffDAO.delete(cardId);
            System.out.println("人员数据删除成功");
        } else if (tableChoice == 2) {
            System.out.print("请输入要删除的书本编号：");
            String bookId = scanner.nextLine().trim();
            bookDAO.delete(bookId);
            System.out.println("图书数据删除成功");
        } else {
            System.out.println("无效选择，请输入1或2");
        }
    }
}
